package com.lemon.components.ant;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;

import com.alibaba.fastjson.JSONArray;
import com.lemon.components.ant.beans.EnumInfo;
import com.lemon.components.ant.beans.TableColumns;
import com.lemon.components.ant.beans.TemplateContext;
import com.lemon.components.ant.util.ClassUtil;
import com.lemon.components.ant.util.EnumConvert;
import com.lemon.components.ant.util.CodeMakerPropertiesUtil;
import com.lemon.components.ant.util.TemplateUtil;

public class ServiceTemplateAntTask extends Task {

	private String projectdir;
	private String serviceName;
	private String servicePackage;
	private String modelName;
	private static Properties p = new Properties();
	static{
		try {
			p.load(HtmlTemplateAntTask.class.getResourceAsStream("/code/codemaker.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void execute() throws BuildException {
		String templatepath = projectdir+"\\src\\main\\resources\\code\\codeTemplate";
		String outputfilepath = projectdir+"\\src\\main\\java\\"+servicePackage;
		log("模版目录："+templatepath,Project.MSG_WARN);
		log("输出："+outputfilepath,Project.MSG_WARN);
		HashMap<String, Object> params = new HashMap<>();
		
		TemplateContext context = new TemplateContext();
		context.setServicePackage(ClassUtil.filepath2classpath(servicePackage));
		context.setServiceName(serviceName);
		context.setModelName(modelName);
		context.setPrimaryKeyMethod(getPrimaryKeyMethod("com.lemon.data.model."+StringUtils.capitalize(context.getModelName())));
		context.setPrimaryKey(p.getProperty("primaryKey"));
		List<EnumInfo> enumlist = EnumConvert.convert(p.getProperty("enums"));
		
		List<TableColumns> columnList = JSONArray.parseArray(CodeMakerPropertiesUtil.get("columns"), TableColumns.class);
		
		context.setColumns(columnList);
		ClassUtil.setType(context.getSearchColumns(), p.getProperty("modelName"));
		params.put("info", context);
		//Service
		TemplateUtil.getInstance(templatepath).build(
				"service/TemplateService.java",
				outputfilepath+"\\"+StringUtils.capitalize(modelName)+"Service.java",
				params
			);
		//Result
		TemplateUtil.getInstance(templatepath).build(
				"service/result/TemplatePageResult.java",
				outputfilepath+"\\result\\"+StringUtils.capitalize(serviceName)+"PageResult.java",
				params
			);
		TemplateUtil.getInstance(templatepath).build(
				"service/result/TemplateResult.java",
				outputfilepath+"\\result\\"+StringUtils.capitalize(serviceName)+"Result.java",
				params
			);
		context.getColumns().forEach(item -> {
			if(item.getSelect()!=null&&item.getSelect().getType().equalsIgnoreCase("enum")){
				TemplateUtil.getInstance(templatepath).build(
						"service/result/TemplateJSONObjectResult.java",
						outputfilepath+"\\result\\JSONObjectResult.java",
						params
					);
			}
		});
		
		//Order
		TemplateUtil.getInstance(templatepath).build(
				"service/order/TemplateOrder.java",
				outputfilepath+"\\order\\"+StringUtils.capitalize(modelName)+"Order.java",
				params
			);
		//Info
		TemplateUtil.getInstance(templatepath).build(
				"service/info/TemplateInfo.java",
				outputfilepath+"\\info\\"+StringUtils.capitalize(modelName)+"Info.java",
				params
			);
		//Imp
		TemplateUtil.getInstance(templatepath).build(
				"service/TemplateServiceImpl.java",
				outputfilepath+"\\impl\\"+StringUtils.capitalize(modelName)+"ServiceImpl.java",
				params
			);
		//Enums
		if(enumlist!=null)
			enumlist.forEach(item -> {
				context.setEnuminfo(item);
				params.remove("info");
				params.put("info", context);
				TemplateUtil.getInstance(templatepath).build(
						"service/enums/TemplateEnums.java",
						outputfilepath+"\\enums\\"+item.getName()+".java",
						params
					);
			});
	}
	
	public String getPrimaryKeyMethod(String str){
		return ClassUtil.getMethodName(str, p.getProperty("primaryKey"));
	}
	
	public String getProjectdir() {
		return projectdir;
	}

	public void setProjectdir(String projectdir) {
		this.projectdir = projectdir;
	}

	public String getServiceName() {
		return serviceName;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	public String getModelName() {
		return modelName;
	}

	public void setModelName(String modelName) {
		this.modelName = modelName;
	}

	public String getServicePackage() {
		return servicePackage;
	}

	public void setServicePackage(String servicePackage) {
		this.servicePackage = servicePackage;
	}

}
